//
//  AGSGeometryParse.m
//  dataGathering
//
//  Created by apple on 14/12/31.
//  Copyright (c) 2014年 shuili. All rights reserved.
//

#import "AGSGeometryParse.h"

@implementation AGSGeometryParse
+(NSString*)GeometryToJson:(AGSGeometry *)geometry
{
    if (geometry==nil) {
        return nil;
    }
    else
    {
        NSDictionary *json = [geometry encodeToJSON];
        NSString* jsonString = [json ags_JSONRepresentation];
        return jsonString;
    }
}
+(AGSGeometry*)JsonToGeometry:(NSString *)json geometype:(NSString *)type{
    NSLog(@"%@",json);
    NSDictionary* jsondic = [json ags_JSONValue];
    AGSGeometry *geo;
    if ([type isEqual:@"Point"]) {
        geo=[[AGSPoint alloc]initWithJSON:jsondic];
    } else if([type isEqual:@"Polyline"])
    {
        geo=[[AGSPolyline alloc]initWithJSON:jsondic];
    }
    else
    {
        geo=[[AGSPolygon alloc] initWithJSON:jsondic];
    }
    
    
    
    return geo;
}

+(NSString*)GeometryToWKT:(collectedData*)data
{
    AGSGeometry *geometry = [data geometry];
    NSString *typeString = @"";
    NSString *wktstring = @"";
    switch (AGSGeometryTypeForGeometry(geometry)) {
        case AGSGeometryTypePoint:
            typeString = @"POINT";
            wktstring = [NSString stringWithFormat:@"%@(%f %f)",typeString, [((AGSPoint*)geometry) x], [((AGSPoint*)geometry) y]];
            break;
        case AGSGeometryTypePolyline:
            typeString = @"LINESTRING";
            wktstring = [NSString stringWithFormat:@"%@(",typeString];
            
            for(int x = 0;x < [(AGSPolyline*)geometry numPoints];x++){
                AGSPoint *point = [(AGSPolyline*)geometry pointOnPath:0 atIndex:x];
                NSString *newpoint = [NSString stringWithFormat:@"%f %f",[point x], [point y]];
                wktstring = [wktstring stringByAppendingString:newpoint];
                if(x != ([(AGSPolyline*)geometry numPoints]-1)){
                    wktstring = [wktstring stringByAppendingString:@","];
                }
            }
            wktstring = [wktstring stringByAppendingString:@")"];
            break;
        case AGSGeometryTypePolygon:
            typeString = @"POLYGON";
            wktstring = [NSString stringWithFormat:@"%@((",typeString];
            
            for(int x = 0;x < [(AGSPolygon*)geometry numPoints];x++){
                AGSPoint *point = [(AGSPolygon*)geometry pointOnRing:0 atIndex:x];
                NSString *newpoint = [NSString stringWithFormat:@"%f %f",[point x], [point y]];
                wktstring = [wktstring stringByAppendingString:newpoint];
                if(x != ([(AGSPolygon*)geometry numPoints]-1)){
                    wktstring = [wktstring stringByAppendingString:@","];
                }
            }
            wktstring = [wktstring stringByAppendingString:@"))"];
            break;
            
        default:
            break;
    }
    
    return wktstring;
    
}


+(AGSGeometry*)TextToGeometry:(NSString *)text
{
    AGSSpatialReference *ref = [AGSSpatialReference spatialReferenceWithWKID:4490];
    AGSGeometry *geo;
    if(text)
    {
        if([text hasPrefix:@"POLYGON"])
        {
            geo = [[AGSMutablePolygon alloc] initWithSpatialReference:ref];
            [(AGSMutablePolygon*)geo addRingToPolygon];
            
            NSString *coordinateString = [text substringWithRange:NSMakeRange(9, ([text length] - 11))];
            NSArray *array = [coordinateString componentsSeparatedByString:@", "];
            for(NSString *perUnit in array)
            {
                NSArray *coordinateArray = [perUnit componentsSeparatedByString:@" "];
                [(AGSMutablePolygon*)geo addPointToRing:[AGSPoint pointWithX:[(NSString*)[coordinateArray objectAtIndex:0] floatValue] y:[(NSString*)[coordinateArray objectAtIndex:1] floatValue] spatialReference:ref]];
            }
        } else if([text hasPrefix:@"LINESTRING"])
        {
            geo = [[AGSMutablePolyline alloc] initWithSpatialReference:ref];
            [(AGSMutablePolyline*)geo addPathToPolyline];
            
            NSString *coordinateString = [text substringWithRange:NSMakeRange(11, ([text length] - 12))];
            NSArray *array = [coordinateString componentsSeparatedByString:@", "];
            for(NSString *perUnit in array)
            {
                NSArray *coordinateArray = [perUnit componentsSeparatedByString:@" "];
                [(AGSMutablePolyline*)geo addPointToPath:[AGSPoint pointWithX:[(NSString*)[coordinateArray objectAtIndex:0] floatValue] y:[(NSString*)[coordinateArray objectAtIndex:1] floatValue] spatialReference:ref]];
            }
        } else
        {
            
            NSString *coordinateString = [text substringWithRange:NSMakeRange(6, ([text length] - 7))];
            NSArray *array = [coordinateString componentsSeparatedByString:@" "];
            
            geo = [AGSPoint pointWithX:[(NSString*)[array objectAtIndex:0] floatValue] y:[(NSString*)[array objectAtIndex:1] floatValue] spatialReference:ref];
        }
    }
    return geo;
}

@end
